package com.acmetelecom.call;

import static com.acmetelecom.call.CallEventBuilder.aCallFrom;
import static com.acmetelecom.call.CallEventBuilder.first;
import static com.acmetelecom.customer.CustomerBuilder.aCustomerNamed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;

import com.acmetelecom.call.Call;
import com.acmetelecom.call.CallEvent;
import com.acmetelecom.call.CallLogger;
import com.acmetelecom.customer.Customer;

public class CallLoggerTest {
	private CallLogger callLogger;
	private DateTime time1 = new DateTime(0);
	private DateTime time2 = new DateTime(40000);
	private DateTime time3 = new DateTime(50000);
	private DateTime time4 = new DateTime(60000);
	
	private Customer jane, chris, tim, bob;
	
	@Before
	public void before() {
		callLogger = new CallLogger();
		jane = aCustomerNamed("Jane").withPhoneNo("+4412345").withThePricePlan("Leisure");
		tim = aCustomerNamed("Tim").withPhoneNo("+4413345").withThePricePlan("Business");
		bob = aCustomerNamed("Bob").withPhoneNo("+4413645").withThePricePlan("Business");
		chris = aCustomerNamed("Chris").withPhoneNo("+4413695").withThePricePlan("Saver");
	}
	
	@Test
	public void testLogStartButNoEnd() {
		callLogger.log(aCallFrom(tim).to(bob).thatStartedAt(time1));
		List<Call> calls = callLogger.getCompletedCalls(tim);
		assertTrue(calls.isEmpty());
	}
	
	@Test
	public void testLogStartToEnd() {
		CallStart start = aCallFrom(tim).to(bob).thatStartedAt(time1);
		CallEnd end = aCallFrom(tim).to(bob).endedAt(time2);
		callLogger.log(start);
		callLogger.log(end);
		assertEquals(Arrays.asList(first(start).then(end)), callLogger.getCompletedCalls(tim));
	}	
	
	@Test
	public void testLogOverlappingCalls() {
		CallStart start1 = aCallFrom(tim).to(bob).thatStartedAt(time1);
		CallEnd end1 = aCallFrom(tim).to(bob).endedAt(time3);
		
		CallStart start2 = aCallFrom(jane).to(chris).thatStartedAt(time2);
		CallEnd end2 = aCallFrom(jane).to(chris).endedAt(time4);
		
		callLogger.log(start1);
		callLogger.log(start2);
		callLogger.log(end1);
		callLogger.log(end2);
		
		assertEquals(Arrays.asList(first(start1).then(end1)), callLogger.getCompletedCalls(tim));
		assertEquals(Arrays.asList(first(start2).then(end2)), callLogger.getCompletedCalls(jane));
	}	
	
	@Test
	public void testRepetition() {
		CallStart start1 = aCallFrom(tim).to(bob).thatStartedAt(time1);
		CallEnd end1 = aCallFrom(tim).to(bob).endedAt(time2);
		
		CallStart start2 = aCallFrom(tim).to(bob).thatStartedAt(time3);
		CallEnd end2 = aCallFrom(tim).to(bob).endedAt(time4);
		
		callLogger.log(start1);
		callLogger.log(end1);
		callLogger.log(start2);
		callLogger.log(end2);
		
		List<Call> calls = callLogger.getCompletedCalls(tim);
		assertEquals(Arrays.asList(first(start1).then(end1), first(start2).then(end2)), calls);
	}
}
